//游戏的管理类

import { game } from "../game/control/game";
import cardClass from "../game/model/cardModel";
import { mapClass } from "../game/model/mapModel";
import eventMgr from "../game/model/playerEvent";
import { playerClass } from "../game/model/playerModel";
import skillModel from "../game/model/skill";
import { timeClass } from "../game/model/timeModel";
import { ccs } from "./services";

export class gameService {

    saveState: boolean = false; //存档状态
    music = null;
    constructor() {
        if(ccs.gf.getLocalData("player")){
            this.saveState = true;
        }
    }
    //播放音乐
    changeMusic() {
        cc.log(cc.audioEngine.isMusicPlaying());
        if(!cc.audioEngine.isMusicPlaying()) {
            cc.audioEngine.resumeMusic();
            ccs.winMgr.showWin("toast", {msg: `音乐开`});
        } else {
            cc.audioEngine.pauseMusic();
            ccs.winMgr.showWin("toast", {msg: `音乐关`});
        }
    }
    loadGame(cb?: Function) {
        if(this.saveState) {
            //读取游戏进度
            //修改游戏的工作配置文件
            // ccs.dataMgr.gameJson = ccs.gf.getLocalData("gameJson");
            this.loadPlayer();
            ccs.winMgr.showWin("toast", { msg: "读取成功" });
            if(cb) {
                cb();
            }
        } else {
            ccs.winMgr.showWin("toast", { msg: "没有存档" });
        }
    }
    loadPlayer() {
        game.playerMgr.heroInfo = ccs.gf.getLocalData("player");
        //读取公司数据，如果不等于当前的长度，则增加少的
        if(game.playerMgr.heroInfo.companyList.length < ccs.dataMgr.getCompanyList().length) {
            game.playerMgr.heroInfo.companyList = game.playerMgr.heroInfo.companyList.concat(ccs.dataMgr.getCompanyList().slice(game.playerMgr.heroInfo.companyList.length));
        }
        if(!game.playerMgr.heroInfo.rank) {
            game.playerMgr.heroInfo.rank = {
                id: 1,
                star: 0,
            }
        }
        game.playerMgr.ads = ccs.gf.getLocalData("ads");
        //证书
        game.cardMgr.cardList = ccs.gf.getLocalData("card");
        game.timerMgr.gameTime = ccs.gf.getLocalData("time");
        //记录
        game.recordMgr.setList(ccs.gf.getLocalData("record"));
        //背包
        let bag = ccs.gf.getLocalData("bag");
        game.playerMgr.bag.loadBag(bag);
        
    }
    saveGame(cb?: Function) {
        this.saveState = true;
        ccs.gf.saveData("player", game.playerMgr.heroInfo);
        //背包数据
        ccs.gf.saveData("bag", game.playerMgr.bag);
        ccs.gf.saveData("ads", game.playerMgr.ads);
        ccs.gf.saveData("card", game.cardMgr.cardList);
        ccs.gf.saveData("record", game.recordMgr.getList());
        ccs.gf.saveData("time", game.timerMgr.gameTime);
        // ccs.winMgr.showWin("toast", { msg: "保存成功" });
        if(cb) {
            cb();
        }
    }
    clearGame() {
        this.saveState = false;
        // game.playerMgr.bag.clearBag();
        ccs.gf.clearData();
    }
    initGame() {
        this.clearGame();
        game.timerMgr = new timeClass(); //游戏时间
        game.skillMgr = new skillModel(); //技能管理
        game.playerMgr = new playerClass(); //玩家类
        game.gameMgr = new gameService();//游戏管理类
        game.mapMgr = new mapClass();//游戏管理类
        game.cardMgr = new cardClass();//游戏管理类
        game.recordMgr = new eventMgr(); //游戏管理类
        //生成关系图
        game.playerMgr.randomParent();
        game.recordMgr.addList({name: "你", action: "出生了,6斤8两"})
    }

}